package com.ddlx.test.binder.aidl;

import java.io.FileDescriptor;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

public class Message implements Parcelable{


	private Bitmap bitmap;
	public Bitmap getBitmap() {
		return bitmap;
	}

	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	private String stringData;
	public String getStringData() {
		return stringData;
	}

	public void setStringData(String stringData) {
		this.stringData = stringData;
	}

	public int getIntData() {
		return intData;
	}

	public void setIntData(int intData) {
		this.intData = intData;
	}

	public float getFloatData() {
		return floatData;
	}

	public void setFloatData(float floatData) {
		this.floatData = floatData;
	}

	public byte[] getBytesData() {
		return bytesData;
	}

	public void setBytesData(byte[] bytesData) {
		this.bytesData = bytesData;
	}

	public FileDescriptor getFdData() {
		return fdData;
	}

	public void setFdData(FileDescriptor fdData) {
		this.fdData = fdData;
	}

	private int intData;
	private float floatData;
	private byte[] bytesData;
	private FileDescriptor fdData;
	
	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(stringData);
		dest.writeInt(intData);
		dest.writeFloat(floatData);
		int byteLen = bytesData==null?0:bytesData.length;
		dest.writeInt(byteLen);
		if (byteLen > 0 ) {
			dest.writeByteArray(bytesData);
		}
		if ( fdData != null ) {
			dest.writeInt(1);
			dest.writeFileDescriptor(fdData);
		}else {
			dest.writeInt(0);
		}
		if ( bitmap != null ) {
			dest.writeInt(1);
			dest.writeParcelable(bitmap, 0);
		}else {
			dest.writeInt(0);
		}
	}
	
	public void readFromParcel(Parcel src) {
		try {
		stringData = src.readString();
		intData = src.readInt();
		floatData = src.readFloat();
		int byteLen = src.readInt();
		if ( byteLen > 0 ) {
			bytesData = new byte[byteLen];
			src.readByteArray(bytesData);
		}
		if ( src.readInt() == 1 ) {
			fdData = src.readFileDescriptor().getFileDescriptor();
		}
		} catch( RuntimeException e) {
			e.printStackTrace();
		}
		if ( src.readInt() == 1 ) {
			bitmap = src.readParcelable(this.getClass().getClassLoader());
		}
	}

	public static final Parcelable.Creator<Message> CREATOR
	    = new Parcelable.Creator<Message>() {
		public Message createFromParcel(Parcel in) {
			Message result = new Message();
		    result.readFromParcel(in);
		    return result;
		}
		
		public Message[] newArray(int size) {
		    return new Message[size];
		}
	};



	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("stringData=").append(stringData).append(",");
		builder.append("intData=").append(intData).append(",");
		builder.append("floatData=").append(floatData).append(",");
		builder.append("bytesData=").append(bytesData==null? null:new String(bytesData)).append(",");
		builder.append("fdData=").append(fdData);
		builder.append("bitmap=").append(bitmap);
		return builder.toString();
	}
}
